/**
 * 
 */
package us.gaaoc.framework.webapp.backingbeans.clerk;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIParameter;
import javax.faces.component.UISelectOne;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.dao.persistenceLogic.PersistInfoIntoCaseRecord;
import us.gaaoc.framework.model.Address;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.CaseRecordStatus;
import us.gaaoc.framework.model.CaseRecordType;
import us.gaaoc.framework.model.Court;
import us.gaaoc.framework.model.DocPageRange;
import us.gaaoc.framework.model.DocumentBinaries;
import us.gaaoc.framework.model.DocumentInstance;
import us.gaaoc.framework.model.DocumentPackage;
import us.gaaoc.framework.model.DocumentStatus;
import us.gaaoc.framework.model.Documents;
import us.gaaoc.framework.model.LegalPreparer;
import us.gaaoc.framework.model.LocalCaseCategory;
import us.gaaoc.framework.model.LocalCaseDivision;
import us.gaaoc.framework.model.LocalCaseFilingType;
import us.gaaoc.framework.model.LocalCaseType;
import us.gaaoc.framework.model.Organizations;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.model.PersonAddress;
import us.gaaoc.framework.model.PersonParticipants;
import us.gaaoc.framework.util.DateUtil;
import us.gaaoc.framework.util.ValidatingTextFile;
import us.gaaoc.framework.util.io.FileUtility;
import us.gaaoc.framework.util.pdf.Concatenate;
import us.gaaoc.framework.util.pdf.PageRangesFromTextTargetsUtility;
import us.gaaoc.framework.util.pdf.SplitPDF;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * @author Sukahda Tapshalkar Mar 26, 2007
 * 
 */
@SuppressWarnings({"unchecked", "unused"})
public class FilingDataAndDocumentUploadPage extends BaseBackingBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = -60848976107909141L;

	/**
	 * Properties
	 */
	private CaseRecord cr;

	private Date courtDate;

	private DataModel personModel;

	private DataModel documentsModel;

	private UploadedFile myFile;

	private List<UploadedFile> myFilesList = null;

	private List<Documents> documentList = new ArrayList<Documents>();

	private List<Documents> newUploadedDocumentList = new ArrayList<Documents>();

	private Filing filing;

	private List<Documents> uploadedDocumentList;

	private DataModel myFilesModel;

	private DataModel establishmentFilingModel;

	private UISelectOne docTypeInput;

	private ArrayList<SelectItem> docTypeDescriptionsItems;

	private boolean renderReviewBtn;

	private String docType;

	private UIParameter documentClicked;

	private UIParameter caseDetailsPage;

	private boolean renderListOfPersonsBtn;

	private List<Person> personsExtractedFromTextList = new ArrayList<Person>();

	private Person cusPerson;

	private PersonAddress cusPersonAddress;
	private Address cusAddress;

	private Person nonCusPerson;

	private PersonAddress nonCusPersonAddress;
	private Address nonCusAddress;

	private List<Person> initiatingPartyPersonsList = new ArrayList<Person>();

	private List<PersonAddress> initiatingPartyPersonAddresses = new ArrayList<PersonAddress>();

	//for uploading packages
	private String packageType;
	private UISelectOne packageTypeInput;
	private ArrayList<SelectItem> packageTypeDescriptionsItems = new ArrayList<SelectItem>();
	private UploadedFile packageDoc;
	private Court court;

	//to capture the local system's tracking of defendants
	private String externalCaseRecordNumber;

	//to capture the social security numbers and the dates of birth for 
	//checking for duplicate persons
	private String cusSSN;

	private String nonCusSSN;

	private List<String> initiatingPartyPersonSSNList;
	
	private Transaction transaction;
	private Session hbSession;
	
	private String courtUuid;
	
	private boolean asExhibit;
	
	/**
	 * Integer to hold the external case category code.
	 * <p>
	 * External Case Category Codes: <br/> 1 = Establishment, OCSS <br/> 2 =
	 * Contempt, OCSS <br/> 3 = Review/Modification, OCSS <br/> 4 = Motion to
	 * Redirect, OCSS <br/> 5 = Motion to Transfer, OCSS <br/> 6 = Registration,
	 * OCSS <br/>
	 * </p>
	 */
	int externalCaseCategoryCode = 0;

	private static final Logger LOG = Logger
			.getLogger(FilingDataAndDocumentUploadPage.class);

	/**
	 * Constructor
	 */
	public FilingDataAndDocumentUploadPage() {
		
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		
		
		String crUuid = (String) sessionMap.get("caseUuid");
		courtUuid = (String) sessionMap.get("courtUuid");
		Court courtFromSession = this.getModelFacade().findCourt(courtUuid);
		this.setCourt(courtFromSession);
		/*
		 * TODO If existing case, pull external case category code from
		 * database. If new case, pull case category code from parameter set on
		 * new filing page in session.
		 */
		/***********************************************************************
		 * External Case Category Codes: 1 = Establishment, OCSS 2 = Contempt,
		 * OCSS 3 = Review/Modification, OCSS 4 = Motion to Redirect, OCSS 5 =
		 * Motion to Transfer, OCSS 6 = Registration, OCSS
		 * ********************************************************************
		 */
		/*
		externalCaseCategoryCode = (Integer) sessionMap
				.get("externalCaseCategoryCode");
		*/
		if (crUuid != null) {
			this.setRenderReviewBtn(true);
			this.setRenderListOfPersonsBtn(true);
			this.setCr(this.getModelFacade().findCaseRecordById(crUuid, courtUuid));
		} else {
			this.setRenderReviewBtn(false);
			this.setRenderListOfPersonsBtn(false);
		}
	}

	/**
	 * @return the docType
	 */
	public String getDocType() {
		return docType;
	}

	/**
	 * @param docType
	 *            the docType to set
	 */
	public void setDocType(String docType) {
		this.docType = docType;
	}

	public UISelectOne getDocTypeInput() {
		return docTypeInput;
	}

	public void setDocTypeInput(UISelectOne docTypeInput) {
		this.docTypeInput = docTypeInput;
	}

	/**
	 * @return docTypeDescriptionsItems
	 */
	public ArrayList<SelectItem> getDocTypeDescriptionsItems() {
		// Initialize Array List if null
		if (docTypeDescriptionsItems == null) {
			docTypeDescriptionsItems = new ArrayList<SelectItem>();
		}
		List<DocumentInstance> docTypeList = this.getModelFacade()
				.findDocumentInstanceByAliasType(court);
		int i = docTypeList.size();
		DocumentInstance dt;
		for (int j = 0; j < i; j++) {
			dt = docTypeList.get(j);
			docTypeDescriptionsItems.add(new SelectItem(new Integer(dt
					.getCode()).toString(), dt.getDescription()));
		}
		return docTypeDescriptionsItems;
	}

	public void setDocTypDescriptionsItems(ArrayList<SelectItem> si) {
		docTypeDescriptionsItems = si;
	}

	/**
	 * @return myFilesModel
	 */
	public DataModel getMyFilesModel() {
		return myFilesModel;
	}

	/**
	 * @param set
	 *            myFilesModel
	 */
	public void setMyFilesModel(DataModel myFilesModel) {
		this.myFilesModel = myFilesModel;
	}

	/**
	 * @return myFilesList
	 */
	public List<UploadedFile> getMyFilesList() {
		return myFilesList;
	}

	/**
	 * @param set
	 *            myFile
	 */
	public void setMyFiles(List<UploadedFile> myFilesList) {
		this.myFilesList = myFilesList;
	}

	/**
	 * @return the myFile
	 */
	public UploadedFile getMyFile() {
		return myFile;
	}

	/**
	 * @param myFile
	 *            the myFile to set
	 */
	public void setMyFile(UploadedFile myFile) {
		this.myFile = myFile;
	}

	/**
	 * @return the courtDate
	 */
	public Date getCourtDate() {
		return courtDate;
	}

	/**
	 * @param courtDate
	 *            the courtDate to set
	 */
	public void setCourtDate(Date courtDate) {
		this.courtDate = courtDate;
	}

	/**
	 * @return documentList
	 */
	public List<Documents> getDocumentList() {
		return documentList;
	}

	/**
	 * @param set
	 *            DocumentTypesByAlias documentList
	 */
	public void setDocumentList(List<Documents> documentList) {
		this.documentList = documentList;
	}

	/**
	 * @return documentsModel
	 */
	public DataModel getDocumentsModel() {
		return documentsModel;
	}

	/**
	 * @param set
	 *            documentsModel
	 */
	public void setDocumentsModel(DataModel documentsModel) {
		this.documentsModel = documentsModel;
	}

	/**
	 * @return personModel
	 */
	public DataModel getPersonModel() {
		return personModel;
	}

	/**
	 * @param set
	 *            personModel
	 */
	public void setPersonModel(DataModel personModel) {
		this.personModel = personModel;
	}

	/**
	 * @return the documentClicked
	 */
	public UIParameter getDocumentClicked() {
		return documentClicked;
	}

	/**
	 * @param documentClicked
	 *            the documentClicked to set
	 */
	public void setDocumentClicked(UIParameter documentClicked) {
		this.documentClicked = documentClicked;
	}

	/**
	 * @return the packageType
	 */
	public String getPackageType() {
		return packageType;
	}

	/**
	 * @param packageType
	 *            the packageType to set
	 */
	public void setPackageType(String packageType) {
		this.packageType = packageType;
	}

	/**
	 * @return the packageTypeDescriptionsItems
	 */
	public ArrayList<SelectItem> getPackageTypeDescriptionsItems() {
		
		// Reinitialization of array to avoid duplicate additions to list.
		

		packageTypeDescriptionsItems = new ArrayList<SelectItem>();
		System.out.println("The Court ID now in process is" + court.getUuid());
		if(court.getUuid().equalsIgnoreCase("courtUuidCoweta"))
		{
			List<DocumentPackage> packageTypeList = this.getModelFacade().findDocumentPackageByCaseRecordAction(court,this.getModelFacade().getCaseRecordActionsById(externalCaseCategoryCode));
			for (DocumentPackage dp : packageTypeList) {
				packageTypeDescriptionsItems.add(new SelectItem(dp.getUuid(), dp
						.getPackageName()));
				LOG.info("The package is .." + dp.getPackageName());
			}
			
			return packageTypeDescriptionsItems;
		}
		else
		{
			List<DocumentPackage> packageTypeList = this.getModelFacade().findDocumentPackageByAliasType(court);
			for (DocumentPackage dp : packageTypeList) {
				packageTypeDescriptionsItems.add(new SelectItem(dp.getUuid(), dp
						.getPackageName()));
				LOG.info("The package is .." + dp.getPackageName());
			}
			
			return packageTypeDescriptionsItems;
		}
		
		
	}

	/**
	 * @param packageTypeDescriptionsItems
	 *            the packageTypeDescriptionsItems to set
	 */
	public void setPackageTypeDescriptionsItems(
			ArrayList<SelectItem> packageTypeDescriptionsItems) {
		this.packageTypeDescriptionsItems = packageTypeDescriptionsItems;
	}

	/**
	 * @return the packageTypeInput
	 */
	public UISelectOne getPackageTypeInput() {
		return packageTypeInput;
	}

	/**
	 * @param packageTypeInput
	 *            the packageTypeInput to set
	 */
	public void setPackageTypeInput(UISelectOne packageTypeInput) {
		this.packageTypeInput = packageTypeInput;
	}

	/**
	 * @return the packageDoc
	 */
	public UploadedFile getPackageDoc() {
		return packageDoc;
	}

	/**
	 * @param packageDoc
	 *            the packageDoc to set
	 */
	public void setPackageDoc(UploadedFile packageDoc) {
		this.packageDoc = packageDoc;
	}

	/**
	 * @return the court
	 */
	public Court getCourt() {
		return court;
	}

	/**
	 * @param court
	 *            the court to set
	 */
	public void setCourt(Court court) {
		this.court = court;
	}
	
	public void uploadFile() {
		
//		if(!lockUploadProcess) {
			try{
				//setLockUploadProcess(true);
				uploadFileSafe();
//				setLockUploadProcess(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
//		}
	}

	/**
	 * upload the document and create case record.
	 * 
	 * @return
	 * @throws Exception
	 *             DocumentTypesByAlias
	 */
	public String uploadFileSafe() throws Exception {
		
		String returnStr = null;
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String caseRecordUuid = (String) sessionMap.get("caseUuid");
		String courtUuid = (String) sessionMap.get("courtUuid");
		Court court = this.getModelFacade().findCourt(courtUuid);

		byte[] fileBytes = myFile.getBytes();
		InputStream inStream = myFile.getInputStream();
		
		// case record already created
		if (caseRecordUuid != null) 
		{
			
			this.setCr(this.getModelFacade().findCaseRecordById(caseRecordUuid, courtUuid));
			if (myFile.getContentType().equalsIgnoreCase("application/pdf")) {
				if (this.getDocType().equalsIgnoreCase("DATA FILE")) {
					LOG.info("Inside the checking of the title of the pdf..");
					FacesContext facesContext = FacesContext
							.getCurrentInstance();
					facesContext
							.addMessage(
									"uploadForm:uploadFile",
									new FacesMessage(
											"DATA FILE is not a title of a pdf's allowed for upload "));
					return "upload";
				}
				
				//Added by Jogi 
				// to Enable Adding the Document as an Exhibit for the Clerk Side
				
				if(this.isAsExhibit())
				{
					Documents toAppend = null;
					boolean docExists = false;
					for(Documents d : this.getCr().getDocuments())
					{
						if(d.getDocumentInstance().getCode() == Integer.parseInt(this.docType)
								&& (d.getStatus().getCode() == 1 || d.getStatus().getCode() == 4 || d.getStatus().getCode() == 6))
						{
							toAppend = d;
							docExists = true;
							break;
						}
					}
					if(docExists)
					{
						File newFile = File.createTempFile(UUID.randomUUID().toString(),".pdf",
								new File("C:" + File.separator + "temp" + File.separator));
						FileOutputStream fw = new FileOutputStream(newFile);
						fw.write(fileBytes);
						fw.flush();
						fw.close();
						
						Concatenate concatenate = new Concatenate();
						concatenate.concatenateHelper(toAppend, newFile.getAbsolutePath());
						
						FacesContext facesContext = this.getFacesContext();
						facesContext.addMessage("uploadForm:uploadFile",
								new FacesMessage(" Appended exhibit to end of the document " + toAppend.getDocumentInstance().getDescription()));
						return "upload";
					}else{
						FacesContext facesContext = this.getFacesContext();
						facesContext.addMessage("uploadForm:uploadFile",
								new FacesMessage(" There is no document named $TITLE to add exhibit to.".replace("$TITLE",
										this.getModelFacade().findDocumentInstanceByCode(Integer.parseInt(this.docType)).getDescription())));																		
						return "failure";
					}
				}
				
				
				Documents doc = new Documents();
				
				DocumentInstance documentInstance = this.getModelFacade()
						.findDocumentInstanceByCode(Integer
								.parseInt(this.docType));
				
				DocumentStatus ds = new DocumentStatus();
				ds = this.getModelFacade().findDocumentStatus(6);
				
				Person personSigned = (Person)sessionMap.get("person");
				String tempTitle = documentInstance.getDescription();
				doc.setTitle(tempTitle.replace(' ', '_')
					.replace(",_", "_").replace('-', '_')
					.replace(':', '_').replace('\'', '_'));
				
				doc.setPhysicalType("pdf");
				doc.setDocumentInstance(documentInstance);
				doc.setReceivedDateTime(DateUtil.getCurrentDate());
				doc.setStatus(ds);
				doc.setPerson(personSigned);
				CaseRecord cr = this.getModelFacade().findCaseRecordById(
						caseRecordUuid, courtUuid);
				doc.setCaseRecord(cr);
				
				this.getModelFacade().makePersistent(doc);
				doc.setContent(null,fileBytes);
			
				List<Documents> listOfDoc = new ArrayList<Documents>();
				listOfDoc = this.getModelFacade().findDocumentsByCaseUuid(
						caseRecordUuid);
			
				CaseRecordStatus cs = this.getModelFacade().findCaseRecordStatusByCode(20); //ready to import
				cr.setStatus(cs);
				
				returnStr = "upload";
			}
			// not a pdf file
			else {
				FacesContext facesContext = FacesContext.getCurrentInstance();
				facesContext
						.addMessage(
								"uploadForm:uploadFile",
								new FacesMessage(
										"A text file has been already uploaded. Please upload a pdf file if there's any. "));
				
				//setLockUploadProcess(false);
				LOG.error("Text file already uploaded");
				returnStr = "failure";
			}
		}

		return returnStr;
	}

	/**
	 * This method navigates user to the Case Details Page
	 * 
	 * @return String
	 */
	public String gotoCaseDetailsPage() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		sessionMap.put("from", "establishmentFilingPage");
		
		return "gotoCaseDetailsPage";
	}

	public String retrieveFilingsForExternalCaseNumber() {
		return "goToSearchPage";
	}

	public String uploadPackage() throws Exception {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String caseRecordUuid = (String) sessionMap.get("caseUuid");
		// case record already created
		if(!packageDoc.getContentType().equalsIgnoreCase("application/pdf")){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage("uploadPackageForm:uploadPack",
					new FacesMessage
						(" File is not a pdf: Please upload a pdf file"));
			return "upload";
		}
		if ((caseRecordUuid != null) && (packageDoc != null)) { 
			byte[] fileBytes = (byte[]) packageDoc.getBytes();
			
			/*
			 * Create temporary file. After using delete from the disk.
			 */
			// TODO - Need to try to stay away from hard coded file paths
			/* This also very likely could be replaced with a file stream object. */
			File newFile =FileUtility.createNewFile(packageDoc.getName(), ".pdf",
					new File("/srv/temp/"),fileBytes);

			setCr(this.getModelFacade().findCaseRecordById(caseRecordUuid, courtUuid));

			/*
			 * Start Section to change Filing type to contempt based on
			 * package type
			 * 
			 * Currently set up for Bibb County only
			 */
			if (this.packageType == "dpBibbUuid2") {
				LocalCaseFilingType localCaseFilingType = this
						.getModelFacade()
						.findLocalCaseFilingTypeByCourtandCode(this.court,
								"121196");
				cr.setLocalCaseFilingType(localCaseFilingType);
			}
			DocumentPackage docPackage = this.getModelFacade()
					.findPackageNameByUuid(this.packageType);
			
			//6 = Uploaded Status
			DocumentStatus ds = this.getModelFacade().findDocumentStatus(6); 
			Person personSigned = (Person) sessionMap.get("person");
			
			Concatenate concatenate = new Concatenate();


			// Determine which court is in use
			//Checking for Bibb County
			if (this.court.getUuid().toString().equalsIgnoreCase("courtUuidBibb")) {
				// append PDF exhibit package to existing PDF documents
				
				//dpUuid14 ="FormCEJ-EX Review - Modification Petition Exhibits"
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid14")) {
					List<Documents> docList = this.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						// 174 = "Petition__Adopt_Agency_Recommendation___FormCEJ___Bibb"
						if (docToAppend.getDocumentInstance().getCode() == 174) { 
								concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());
								FacesContext facesContext = FacesContext.getCurrentInstance();
								facesContext.addMessage("uploadPackageForm:uploadPack",
										new FacesMessage(" Package has been appended"));
						}
					}
				}
				
				// "FormCAG-EX - Motion To Redirect Exibits" =dpUuid18
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid18")) {
					
					
					List<Documents> docList = this
							.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						//Motion to Redirect
						if (docToAppend.getDocumentInstance().getCode() == 179) { 
							//"Petition__Adopt_Agency_Recommendation___FormCEJ___Bibb" = 179
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}
				//"FormCAH-EX - Motion To Transfer Exibits" = dpUuid19
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid19")) {
					List<Documents> docList = this
							.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						//Motion to Transfer with Attached Order = 41
						if (docToAppend.getDocumentInstance().getCode() == 41) { 
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}
				//"FormCCU-EX - Notice of Registration Exibits" = dpUuid20
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid20")) {
					List<Documents> docList = this.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						//"Notice: Of Registration" =182
						if (docToAppend.getDocumentInstance().getCode() == 182) {
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}
			} else if (this.court.getUuid().toString().equalsIgnoreCase("courtUuid2")) {
				//Form CEJ-EX Rev/Mod Exhibits = dpUuid21
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid21")) {																			
					List<Documents> docList = this
							.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						// Petition to Adopt Agency Recommendation- CEJ = 135
						if (docToAppend.getDocumentInstance().getCode() == 135) {
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());	
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}
				// FormCCP-EX - Petition for Additional Children Exhibits = dpUuid23
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid23")) { 
					List<Documents> docList = this
							.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						// Petition to Adopt Agency Recommendation - CEJ = 98
						if (docToAppend.getDocumentInstance().getCode() == 98) {
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}
				//FormCAB-EX Motion Contempt Exhibits = dpUuid24
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid24")) { 
					List<Documents> docList = this
							.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						// Petition to Adopt Agency Recommendation - CEJ =  114
						if (docToAppend.getDocumentInstance().getCode() == 114) {
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());	
							
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}
				//FormCAA-EX Motion Contempt Exhibits = dpUuid5a
				if (docPackage.getUuid().equalsIgnoreCase("dpBibbUuid5a")) { 
					List<Documents> docList = this
							.getUploadedDocumentList();
					for (Documents docToAppend : docList) {
						// Motion For Contempt =  48
						if (docToAppend.getDocumentInstance().getCode() == 48) {
							concatenate.concatenateHelper(docToAppend, newFile.getAbsolutePath());	
							
							FacesContext facesContext = FacesContext.getCurrentInstance();
							facesContext.addMessage("uploadPackageForm:uploadPack",
									new FacesMessage(" Package has been appended"));
						}
					}
				}

			}else if (this.court.getUuid().toString().equalsIgnoreCase(
					"courtUuid3"))  // douglas
			{

				
			}if (court.getUuid().equals("courtUuidHart"))
			{
				
			} else if (this.court.getUuid().toString().equalsIgnoreCase("courtUuidJefferson")) {
			

			}
				// End Append Method by Frank

				this.splitPDFPackage(docPackage, newFile, caseRecordUuid,
						personSigned);
				//delete the file created on the disk for security reason
				
				this.getCr().setStatus(this.getModelFacade().findCaseRecordStatusByCode(20));
				
				
				newFile.delete();
		//	}
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage("uploadPackageForm:uploadPack",
					new FacesMessage(" Package is Uploaded and Separated"));

		} else { // not a data file
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.addMessage("uploadPackageForm:uploadPack",
					new FacesMessage(" Please upload a data file first "));
		}
		return "upload";
	}

	public String ViewUploadedFileInformation() {
		this.documentList = this.getModelFacade().findDocumentsByDocStatusCode(
				6);
		documentsModel = new ListDataModel();
		documentsModel.setWrappedData(documentList);
		return "viewUploadedFileInformation";
	}

	public void viewListOfUploadedFiles() {
		// To avoid duplicate additions to arrays, reinitialize with each call.
		uploadedDocumentList = new ArrayList<Documents>();
		documentList = new ArrayList<Documents>();
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String uuid = (String) sessionMap.get("caseUuid");
		this.documentList = this.getModelFacade().findDocumentsByCaseUuid(uuid);
		for (Documents doc : documentList) {
			if (doc.getStatus().getDescription().equalsIgnoreCase("upload")) {
				LOG.info("Adding the document " + doc.getTitle()
						+ " to the uploaded dcoument list");
				this.uploadedDocumentList.add(doc);

			}
		}
	}

	/**
	 * @return the uploadedDocumentList
	 */
	public List<Documents> getUploadedDocumentList() {
		// To avoid duplicate additions to array, reinitialize with each call.
		uploadedDocumentList = new ArrayList<Documents>();
		documentList = new ArrayList<Documents>();
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String uuid = (String) sessionMap.get("caseUuid");
		if (uuid != null) {
			List<Documents> documentList = this.getModelFacade()
					.findDocumentsByCaseUuid(uuid);
			for (Documents doc : documentList) {
				//Added by Frank
				int docStatusCode = doc.getStatus().getCode();
				//if (doc.getStatus().getDescription().equalsIgnoreCase("upload")) {
				if (docStatusCode == 6 || docStatusCode == 8 || docStatusCode == 11 || docStatusCode == 15 ) {
					this.uploadedDocumentList.add(doc);
				}
			}
		}
		return uploadedDocumentList;
	}

	/**
	 * @param uploadedDocumentList
	 *            the uploadedDocumentList to set
	 */
	public void setUploadedDocumentList(List<Documents> uploadedDocumentList) {
		this.uploadedDocumentList = uploadedDocumentList;
	}
	
	public String viewFile() {
		FacesContext context = this.getFacesContext();
		HttpServletResponse resp = (HttpServletResponse) FacesContext
				.getCurrentInstance().getExternalContext().getResponse();
		Documents doc = (Documents) this.getDocumentClicked().getValue();
		String docTitle = (String) doc.getTitle();
		byte[] docContent = doc.getContent();
		try {
			FileUtility.streamReportToBrowser(resp, docTitle, docContent);
		} catch (IOException e) {
			LOG.error("Error in FilingDataUploadPage.viewFile() method: " + e);
		}
		context.responseComplete();
		return "stay";
	}

	public String reviewOfFiling() {
		LOG.info("Inside the reviewOfFiling method....");
		
		return "reviewOfFiling";
	}


	public CaseRecord getCr() {
		return cr;
	}

	public void setCr(CaseRecord cr) {
		this.cr = cr;
	}

	public List<Documents> getNewUploadedDocumentList() {
		return newUploadedDocumentList;
	}

	public void setNewUploadedDocumentList(
			List<Documents> newUploadedDocumentList) {
		this.newUploadedDocumentList = newUploadedDocumentList;
	}

	public UIParameter getCaseDetailsPage() {
		return caseDetailsPage;
	}

	public void setCaseDetailsPage(UIParameter caseDetailsPage) {
		this.caseDetailsPage = caseDetailsPage;
	}

	public DataModel getEstablishmentFilingModel() {
		return establishmentFilingModel;
	}

	public void setEstablishmentFilingModel(DataModel establishmentFilingModel) {
		this.establishmentFilingModel = establishmentFilingModel;
	}

	public Filing getFiling() {
		return filing;
	}

	public void setFiling(Filing filing) {
		this.filing = filing;
	}

	public String deleteDocument() {
		LOG.info("Inside the deleteDocument..");
		/* ********************************************************
		 * TODO - Important - Need to make sure that delete of 
		 * persons does not delete persons that are present in 
		 * other cases.
		 * ******************************************************
		 */
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		Documents doc = (Documents) this.getDocumentClicked().getValue();
		CaseRecord cr = this.getCr();
		String extention = doc.getPhysicalType();
		if (extention.equalsIgnoreCase("text")) {
			List<PersonParticipants> ppList = this.getModelFacade()
					.getPersonParicipant(cr);
			for (PersonParticipants pp : ppList) {
				Person personParticipant = (Person) this.getModelFacade()
						.findPersonbyID(pp.getPerson().getUuid());
				List<CaseRecord> crList = this.getModelFacade()
						.findCaseRecordsByPerson(personParticipant);
				LOG.info("List of related case Records for the PP "
						+ crList.size());
				if ((personParticipant.getRoles().isEmpty())
						&& (crList.size() <= 1)) {
					this.getModelFacade().deletePerson(pp.getPerson());

				} else {
					this.getModelFacade().deletePersonParticipants(pp);
				}
			}
			this.getModelFacade().deleteCaseRecord(this.getCr().getUuid(), courtUuid);
			sessionMap.remove("caseUuid");
		}

		this.getModelFacade().enableDocDeleteFlag(doc);
		this.getModelFacade().deleteDocument(doc);
		
		return "upload";
	}

	public String back() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		sessionMap.put("caseUuid",this.getCr().getUuid());
		return "back";
	}

	public boolean isRenderReviewBtn() {
		return renderReviewBtn;
	}

	public void setRenderReviewBtn(boolean renderReviewBtn) {
		this.renderReviewBtn = renderReviewBtn;
	}

	public boolean validateAttorney(Person personAttorney) {
		// Attorney attorney = this.getModelFacade().
		return false;
	}

	public boolean isRenderListOfPersonsBtn() {
		return renderListOfPersonsBtn;
	}

	public void setRenderListOfPersonsBtn(boolean renderListOfPersonsBtn) {
		this.renderListOfPersonsBtn = renderListOfPersonsBtn;
	}

	public List<Person> getPersonsExtractedFromTextList() {
		return personsExtractedFromTextList;
	}

	public void setPersonsExtractedFromTextList(
			List<Person> personsExtractedFromTextList) {
		this.personsExtractedFromTextList = personsExtractedFromTextList;
	}

	public Person getCusPerson() {
		return cusPerson;
	}

	public void setCusPerson(Person cusPerson) {
		this.cusPerson = cusPerson;
	}

	public Person getNonCusPerson() {
		return nonCusPerson;
	}

	public void setNonCusPerson(Person nonCusPerson) {
		this.nonCusPerson = nonCusPerson;
	}
	public PersonAddress getCusPersonAddress() {
		return cusPersonAddress;
	}

	public void setCusPersonAddress(PersonAddress cusPersonAddress) {
		this.cusPersonAddress = cusPersonAddress;
	}
	public PersonAddress getNonCusPersonAddress() {
		return nonCusPersonAddress;
	}

	public void setNonCusPersonAddress(PersonAddress nonCusPersonAddress) {
		this.nonCusPersonAddress = nonCusPersonAddress;
	}
	
	public List<Person> getInitiatingPartyPersonsList() {
		return initiatingPartyPersonsList;
	}

	public void setInitiatingPartyPersonsList(
			List<Person> initiatingPartyPersonsList) {
		this.initiatingPartyPersonsList = initiatingPartyPersonsList;
	}

	public List<PersonAddress> getInitiatingPartyPersonAddresses() {
		return initiatingPartyPersonAddresses;
	}

	public void setInitiatingPartyPersonAddresses(
			List<PersonAddress> initiatingPartyPersonAddresses) {
		this.initiatingPartyPersonAddresses = initiatingPartyPersonAddresses;
	}

	/**
	 * 
	 * @param documentInstance
	 * @param newFile
	 * @param caseRecordUuid
	 * @param personSigned
	 * @throws IOException
	 */

	private void splitPDFPackage(DocumentPackage documentPackage, File newFile,
			String caseRecordUuid, Person personSigned) throws IOException {

		DocumentStatus ds = new DocumentStatus();
		ds = this.getModelFacade().findDocumentStatus(6);

		// Pull List of Documents and Page Ranges from DB
		List<DocPageRange> dprList = this.getModelFacade()
				.findAllPageRangeByPackageUuid(documentPackage);

		/*
		 * Check the document for dynamically set page range indicators.
		 * 
		 * If present, over ride page ranges from the database. If not present,
		 * then leave list of documents and page ranges unchanged.
		 */
		PageRangesFromTextTargetsUtility targetBasedRangeChecker =
			new PageRangesFromTextTargetsUtility();

		// Convert file to Byte Array
		FileInputStream fis = FileUtility.convertFileToInputStream(newFile);

		List<DocPageRange> dprUpdatedList = targetBasedRangeChecker
				.pageRangesFromTargetsCheck(fis, dprList);
		for (DocPageRange dpr : dprUpdatedList) {
					Hibernate.initialize(dpr.getDocumentInstance());
		}

		SplitPDF sp = new SplitPDF();

		ByteArrayOutputStream[] baosArray = sp.SplitFile("C:" + File.separator + "temp" + File.separator
				+ newFile.getName(), dprUpdatedList);
		
		int i = 0;
		for (DocPageRange dn : dprUpdatedList) {
			Documents tempDoc = new Documents();
			DocumentInstance docInstance = new DocumentInstance();
			docInstance = dn.getDocumentInstance();
			
			String fileTitle = (dn.getDocumentInstance().getDescription()
					.toString().replace(' ', '_').replace("-", "_").replace(
							",_", "_").replace(':', '_').replace('\'', '_'));

			tempDoc.setTitle(fileTitle);
			tempDoc.setPhysicalType("pdf");
			CaseRecord cr = this.getModelFacade().findCaseRecordById(
					caseRecordUuid, courtUuid);
			tempDoc.setCaseRecord(cr);
			tempDoc.setDocumentInstance(docInstance);
			tempDoc.setPerson(personSigned);
			tempDoc.setReceivedDateTime(DateUtil.getCurrentDate());
			tempDoc.setStatus(ds);

			this.getModelFacade().makePersistent(tempDoc);
			
			byte[] docContent = baosArray[i].toByteArray();
			tempDoc.setContent(null,docContent);
			
			i++;
		}
	}
	
	public String getExternalCaseRecordNumber() {
		return this.externalCaseRecordNumber;
	}

	public void setExternalCaseRecordNumber(String externalCaseRecordNumber) {
		this.externalCaseRecordNumber = externalCaseRecordNumber;
	}

	public String getCusSSN() {
		return cusSSN;
	}

	public void setCusSSN(String cusSSN) {
		this.cusSSN = cusSSN;
	}

	public String getNonCusSSNHashed() {
		return nonCusSSN;
	}

	public void setNonCusSSNHashed(String nonCusSSNHashed) {
		this.nonCusSSN = nonCusSSNHashed;
	}

	public List<String> getInitiatingPartyPersonSSNList() {
		return initiatingPartyPersonSSNList;
	}

	public void setInitiatingPartyPersonSSNList(
			List<String> initiatingPartyPersonSSNList) {
		this.initiatingPartyPersonSSNList = initiatingPartyPersonSSNList;
	}

	/**
	 * @return the cusAddress
	 */
	public Address getCusAddress() {
		return cusAddress;
	}

	/**
	 * @param cusAddress the cusAddress to set
	 */
	public void setCusAddress(Address cusAddress) {
		this.cusAddress = cusAddress;
	}

	/**
	 * @return the nonCusAddress
	 */
	public Address getNonCusAddress() {
		return nonCusAddress;
	}

	/**
	 * @param nonCusAddress the nonCusAddress to set
	 */
	public void setNonCusAddress(Address nonCusAddress) {
		this.nonCusAddress = nonCusAddress;
	}
	
	
	public boolean isAsExhibit() {
		return asExhibit;
	}

	public void setAsExhibit(boolean asExhibit) {
		this.asExhibit = asExhibit;
	}
	
}
